/**
 *@Description 运单首页
 *@Author zhangnf
 *@Date 2015-09-23
 */
var tsBottomCtrl  = new XFApp.client();
XFApp.client.prototype.bottom = {
	successCode : "10000",
	page : null,
	urlItems : {
		queryByPageURL : basePath+"/nt/transBottom/list",
		exportDataURL : basePath + "/nt/transBottom/exportData",
		delBottomURL : basePath+"/nt/transBottom/delTransBottom",
		dowonLoadURL: basePath+"/nt/transBottom/download"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			currentPageIndex : Number($("#currentPageIndex").val())-1,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(), 
		        success: function (data, pageIndex) {
		        	$("#currentPageIndex").val(data.result.pageNo);
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
						webDialogUtil.simpleAlert("数据查询失败!");
		        	}
		        },
		        error : function(){
					webDialogUtil.simpleAlert("数据查询失败!");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	exportData : function(){
		_this = this;
		$.ajax({
			url:_this.urlItems.exportDataURL,
			data:$('#searchForm').serialize(),
			type:'get',
			cache:false,
			success:function(data) {
				if(data.success){
					var url = _this.urlItems.dowonLoadURL+"?fileName="+data.fileName;
					$("body").append("<iframe id='file_download_frame' src='" + url +"' frameborder=0 width=0 height=0 style='display:none'></iframe>");
				}else{
					window.webDialogUtil.alert("提示",data.errMsg);
				}
			},
			error : function() {
				window.webDialogUtil.alert("提示","操作失败");
			}
		});
	},
	showModal : function(id,type){
		$('#editModal').modal('show');
	},
	delBottomDialog : function(id){
		_this = this;
		webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){tsBottomCtrl.bottom.delBottom(id);});
	},
	delBottom : function(id){
		_this = this;
		$.ajax({
		    url:_this.urlItems.delBottomURL,
		    data:{id:id},  
		    type:'post',  
		    cache:false,  
		    dataType:'json',  
		    success:function(data) {
		    	if(true == data.isSuccess){
					webDialogUtil.simpleAlert(data.msg);
		    		tsBottomCtrl.bottom.queryPage();
		    	}
		     },  
		     error : function() {
				 webDialogUtil.simpleAlert("操作失败!");
		     }
		});
	},
	event : function(){
		_this = this;
		$("#searchButton").on("click",function(){
			tsBottomCtrl.bottom.queryPage();
		});
		$("#exportButton").on("click",function(){
			tsBottomCtrl.bottom.exportData();
		});
		tsBottomCtrl.bottom.queryPage();
	}
};
$.views.converters({
	ftime : function(value) {
		return  new Date(value*1000).format("yyyy-MM-dd hh:mm:ss");
	},
	fcustom : function(value){
		if(!value){
			return "";
		}
		for(index in customs){
			var custom = customs[index];
			if(custom.dictCode == value){
				return custom.dictName;
			}
		}
		return  "未知海关("+value+")";
	},
	fcountry : function(value){
		if(!value){
			return "";
		}
		for(index in countrys){
			var currency = countrys[index];
			if(currency.dictCode == value){
				return currency.dictName;
			}
		}
		return  "未知国家("+value+")";
	}
});
$(function(){
	dictCtrl.dictionary.load("toDocketStatus","docketStatusHidden");
	tsBottomCtrl.bottom.event();
});